package com.jackrain.nea.fc.pos.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.common.PostDubbleEvent;
import com.jackrain.nea.common.PostHandleEvent;
import com.jackrain.nea.common.QueryCpUtil;
import com.jackrain.nea.common.QueryUserUtil;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.dl.common.utils.esutil.ConvertQueryListUtil;
import com.jackrain.nea.dl.common.utils.esutil.ElasticsearchUtil;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.pos.api.ViewDLRetailSaveCmd;
import com.jackrain.nea.fc.pos.mapper.ViewDLRetailMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.face.User;
import com.jackrain.nea.web.query.DefaultWebEvent;
import com.jackrain.nea.web.query.QuerySessionImpl;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import com.jackrain.nea.fc.utils.Constants;

/**
 * 零售单 F4->中间表->FC
 *
 * @author wuwenxue
 * @date 2018/05/17
 **/
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "pos")
public class ViewDLRetailSaveCmdImpl extends CommandAdapter implements ViewDLRetailSaveCmd {

    @Autowired
    PostDubbleEvent postDubbleEvent;

    @Autowired
    PostHandleEvent postHandleEvent;

    @Autowired
    private QueryUserUtil queryUserUtil;

    @Autowired
    private ElasticsearchUtil esUtil;

    @Autowired
    private ConvertQueryListUtil convertQLUtil;

    @Autowired
    private PropertiesConf pconf;

    private Logger accesLogger = LoggerFactory.getLogger(Constants.ACCESS_PARAM_LOGGER);

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {

        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"),
                "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        log.debug("ViewDLRetailSaveCmdImpl-->param-->{}", param);

        if (null == param || param.size() == 0) {
            throw new NDSException("参数格式错误！");
        }

        ViewDLRetailMapper viewDLRetailMapper = ApplicationContextHandle.getBean(ViewDLRetailMapper.class);

        JSONArray paramArray = param.getJSONArray("data");

        JSONArray errorArray = new JSONArray();
        JSONArray spendTimeArray = new JSONArray();

        List succIdList = new ArrayList();

        for (int i = 0; i < paramArray.size(); i++) {
            JSONObject errorJson = new JSONObject();
            JSONObject tranJson = paramArray.getJSONObject(i);
            JSONObject zbJS = tranJson.getJSONObject("DL_T_RETAIL_TRANSFER");
            try {
                JSONObject spendTime = new JSONObject();
                int statu = 0;
                Long retObjid = null;
                // check id
                String bill_No = zbJS.getString("BILL_NO");
                String idList = zbJS.getString("IDLIST");

                int count = viewDLRetailMapper.selectReatilCount(bill_No);
                if (count != 0) {
                    statu = statu + 1;
                }

                //---user
                //保存用户
                String saveUserName = zbJS.getString("OWNERNAME");
                User saveUser = queryUserUtil.getUser(saveUserName);
                //收银用户
                String statusName = zbJS.getString("STATUSNAME");
                User statusUser = queryUserUtil.getUser(statusName);
                //记账用户
                String accountUserName = zbJS.getString("OPR_ECODE");
                User accountUser = queryUserUtil.getUser(accountUserName);

                String autocharge = zbJS.getString("AUTO_CHARGE");
                tranJson.getJSONObject("DL_T_RETAIL_TRANSFER").remove("AUTO_CHARGE");

                //save
                String noticSave = "com.jackrain.nea.retail.api.DlRetailSaveCmd:1.0:pos";
                //收银
                String noticCharge = "com.jackrain.nea.retail.api.DlRetailChargeCmd:1.0:pos";
                //记帐
                String noticAccount = "com.jackrain.nea.retail.api.DlRetailAccountCmd:1.0:pos";

                //0,初始状态（未保存）  1，已保存   2，已收银
                if (0 == statu) {
                    long saveStartTime = System.currentTimeMillis();
                    QuerySessionImpl querySession = new QuerySessionImpl(saveUser);
                    DefaultWebEvent event = new DefaultWebEvent("dlRetail", new HashMap(16));

                    JSONObject resultJson = retailSaveData(tranJson, viewDLRetailMapper);
                    accesLogger.info("ViewDLRetailSaveCmdImpl-save-resultJson-->" + bill_No + "{}", resultJson);

                    event.put("param", resultJson);
                    querySession.setEvent(event);
                    JSONObject retJo = postDubbleEvent.executePostDubble(noticSave, querySession);
                    accesLogger.info("ViewDLRetailSaveCmdImpl-save-retJo-->{}", retJo);

                    double saveEndTime = (double) (System.currentTimeMillis() - saveStartTime) / 1000.0D;
                    spendTime.put("saveTime", saveEndTime);
                    spendTime.put("saveRetData", retJo);
//                    log.debug("saveTime->" + saveEndTime);
//                    log.debug("DlRetailSave_retJo->" + retJo);
                    if (retJo.containsKey("code") && (int) retJo.get("code") != 0) {
                        errorJson.put("key", idList);
                        if (null == retJo.get("message")) {
                            errorJson.put("message", "调用标准零售保存服务异常");
                        } else {
                            errorJson.put("message", "零售保存服务异常:" + retJo.get("message") +
                                    " retData->" + retJo.get("data"));
                        }
                        errorArray.add(errorJson);
                        continue;
                    }
                    JSONObject retData = retJo.getJSONObject("data");
                    retObjid = retData.getLong("objid");

                    succIdList.add(idList);
                    spendTimeArray.add(spendTime);
                } else {
                    retObjid = viewDLRetailMapper.selectObjidByBillNo(zbJS.getString("BILL_NO"));
                }

                //调用收银
                if (!"0".equals(autocharge)) {

                    long chargeStartTime = System.currentTimeMillis();
                    QuerySessionImpl chargeQuerySession = new QuerySessionImpl(statusUser);
                    DefaultWebEvent event = new DefaultWebEvent("dlRetail", new HashMap(16));
                    JSONObject chargeJson = retailChargeData(retObjid);
                    chargeJson.put("STATUSTIME", zbJS.getString("STATUSTIME"));
                    accesLogger.info("ViewDLRetailSaveCmdImpl--chargeJson-->{}", chargeJson);

                    event.put("param", chargeJson);
                    event.setParameter("command", noticCharge);
                    event.setParameter("table", "DL_B_RETAIL");
                    event.setParameter("webaction", "OBJECT_RETAIL_CHARGE");
                    chargeQuerySession.setEvent(event);

                    //handle调用收银
                    JSONObject retChargeJo = postHandleEvent.executePostHandle(chargeJson, chargeQuerySession, event, "webaction");
                    accesLogger.info("ViewDLRetailSaveCmdImpl--retChargeJo-->{}", retChargeJo);

                    double chargeEndTime = (double) (System.currentTimeMillis() - chargeStartTime) / 1000.0D;
                    spendTime.put("chargeTime", chargeEndTime);
                    spendTime.put("chargeRetData", retChargeJo);
//                    log.debug("chargeTime->" + chargeEndTime);
//                    log.debug("DlRetailChargeCmd_handle_retJo->" + retChargeJo);
                    if (retChargeJo.containsKey("code") && (int) retChargeJo.get("code") != 0) {
                        errorJson.put("key", idList);
                        if (null == retChargeJo.get("message")) {
                            errorJson.put("message", "调用标准零售收银服务异常");
                        } else {
                            errorJson.put("message", "零售收银服务异常:" + retChargeJo.get("message"));
                        }
                        errorArray.add(errorJson);
                        continue;
                    }
                    chargeJson.remove("STATUSTIME");

                    //调用记账
//                if (2 == statu) {
                    long accountStartTime = System.currentTimeMillis();
                    QuerySessionImpl accountQuerySession = new QuerySessionImpl(accountUser);
                    DefaultWebEvent accountevent = new DefaultWebEvent("dlRetail", new HashMap(16));
                    JSONObject accountJson = retailChargeData(retObjid);

                    accountevent.put("param", accountJson);
                    accountevent.setParameter("command", noticAccount);
                    accountevent.setParameter("webaction", "OBJECT_RETAIL_ACCOUNT");
                    accountevent.setParameter("isSendMq", "N");
                    accountQuerySession.setEvent(accountevent);
                    accesLogger.info("ViewDLRetailSaveCmdImpl--accountJson-->{}", accountJson);
//                    log.info("account_resultJson==>" + accountJson.toString());
                    //调用记账服务
                    JSONObject retAccountJo = postHandleEvent.executePostHandle(accountJson, accountQuerySession, accountevent, "webaction");
                    accesLogger.info("ViewDLRetailSaveCmdImpl--retAccountJo-->{}", retAccountJo);

                    double accountEndTime = (double) (System.currentTimeMillis() - accountStartTime) / 1000.0D;
                    spendTime.put("accountTime", accountEndTime);
                    spendTime.put("accountRetData", retAccountJo);
//                    log.debug("accountTime->" + accountEndTime);
//                    log.info("DlRetailAccountCmd_retJo->" + retAccountJo);
                    if (retAccountJo.containsKey("code") && (int) retAccountJo.get("code") != 0) {
                        errorJson.put("key", idList);
                        if (null == retAccountJo.get("message")) {
                            errorJson.put("message", "调用标准记账收银服务异常");
                        } else {
                            errorJson.put("message", "零售记账服务异常:" + retAccountJo.get("message"));
                        }
                        errorArray.add(errorJson);
                        continue;
                    }

                    succIdList.add(idList);
                    spendTimeArray.add(spendTime);

                }

            } catch (Exception e) {
                String error = "";
                if ((e instanceof NDSException)) {
                    error = e.getMessage();
                } else {
                    StringWriter sw = new StringWriter();
                    PrintWriter pw = new PrintWriter(sw);
                    e.printStackTrace(pw);
                    error = sw.toString();
                }
                String idList = zbJS.getString("IDLIST");
                errorJson.put("key", idList);
                errorJson.put("message", error);
                errorArray.add(errorJson);
            }
        }

        if (errorArray.size() > 0) {
            vh.put("error", errorArray);
            vh.put("code", -1);
        } else {
            vh.put("code", 0);
        }
        vh.put("successIds", StringUtils.collectionToDelimitedString(succIdList, ","));
        vh.put("spendTime", spendTimeArray);
        return vh;
    }


    /**
     * @param retObjid 头标ID
     * @return
     * @throws Exception
     */
    public JSONObject retailChargeData(Long retObjid) throws Exception {
        JSONObject chargeJson = new JSONObject();
        JSONObject subParam = new JSONObject();
        subParam.put("idArr", new JSONArray());
        subParam.put("table", "DL_B_RETAIL_ITEM");

        chargeJson.put("objid", retObjid);
        chargeJson.put("IS_F4CHECK", "N");
        chargeJson.put("table", "DL_B_RETAIL");
        chargeJson.put("subParam", subParam);
        chargeJson.put("IS_SEND_ACCOUNT_MQ", "unsend");

        return chargeJson;
    }


    public JSONObject retailSaveData(JSONObject tranJson, ViewDLRetailMapper viewDLRetailMapper) throws Exception {

        JSONObject zbJS = tranJson.getJSONObject("DL_T_RETAIL_TRANSFER");
        JSONArray itemJS = tranJson.getJSONArray("DL_T_RETAIL_ITEM_TRANSFER");
        JSONArray payitemJS = tranJson.getJSONArray("DL_T_RETAIL_PAY_TRANSFER");

        /*************处理明细表数据************/

        for (int j = 0; j < itemJS.size(); j++) {
            JSONObject itemJson = itemJS.getJSONObject(j);
            itemJson.put("ID", -1);
            itemJson.put("ISACTIVE", "Y");

            //商品ID
            if (null != itemJson.getString("PS_C_SKU_ECODE")) {
                String skuEcode = itemJson.getString("PS_C_SKU_ECODE");
                //跨库查询
                JSONObject whereJs = new JSONObject();
                whereJs.put("ECODE", skuEcode);
                whereJs.put("CP_C_DISTRIB_ID", 0);
                HashMap h = QueryCpUtil.queryCP("PS_C_SKU",
                        "PS_C_PRO_ID,PS_C_SPEC1OBJ_ID,PS_C_SPEC2OBJ_ID,ID", whereJs);

                if (null != h && h.size() > 0) {
                    //条码ID
                    itemJson.put("PS_C_SKU_ID", h.get("ID"));

                    if (null != h.get("PS_C_SPEC1OBJ_ID")) {
                        //颜色ID
                        itemJson.put("PS_C_CLR_ID", h.get("PS_C_SPEC1OBJ_ID"));
                        //跨库查询
                        JSONObject whereSpecObj = new JSONObject();
                        whereSpecObj.put("ID", h.get("PS_C_SPEC1OBJ_ID"));
                        HashMap specobj = QueryCpUtil.queryCP("PS_C_SPECOBJ",
                                "ENAME,ECODE", whereSpecObj);

                        //颜色编码
                        itemJson.put("PS_C_CLR_ECODE", specobj.get("ECODE"));
                        //颜色名称
                        itemJson.put("PS_C_CLR_ENAME", specobj.get("ENAME"));
                    }

                    if (null != h.get("PS_C_SPEC2OBJ_ID")) {
                        //尺寸ID
                        itemJson.put("PS_C_SIZE_ID", h.get("PS_C_SPEC2OBJ_ID"));

                        //跨库查询
                        JSONObject whereSpecObj = new JSONObject();
                        whereSpecObj.put("ID", h.get("PS_C_SPEC2OBJ_ID"));
                        HashMap specobj = QueryCpUtil.queryCP("PS_C_SPECOBJ",
                                "ENAME,ECODE", whereSpecObj);

                        //尺寸编码
                        itemJson.put("PS_C_SIZE_ECODE", specobj.get("ECODE"));
                        //尺寸名称
                        itemJson.put("PS_C_SIZE_ENAME", specobj.get("ENAME"));
                    }
                } else {
                    throw new NDSException("SKU 查询为空！  sku编码：" + skuEcode);
                }

                if (null != h.get("PS_C_PRO_ID")) {
                    //商品ID
                    itemJson.put("PS_C_PRO_ID", h.get("PS_C_PRO_ID"));
                    //跨库查询
                    JSONObject whereProJs = new JSONObject();
                    whereProJs.put("ID", h.get("PS_C_PRO_ID"));
                    whereProJs.put("CP_C_DISTRIB_ID", 0);
                    HashMap proHashMap = QueryCpUtil.queryCP("PS_C_PRO",
                            "ECODE,ENAME,NUMDIM26,PROMOTIONTYPE,PRONATURE", whereProJs);

                    if (null != proHashMap && proHashMap.size() > 0) {
                        //款号
                        itemJson.put("PS_C_PRO_ECODE", proHashMap.get("ECODE"));
                        //商品名称
                        itemJson.put("PS_C_PRO_ENAME", proHashMap.get("ENAME"));
                        //品类ID
                        itemJson.put("PRO_TYPE_DEPART_ID", proHashMap.get("NUMDIM26"));

                        Long proMotion = TypeUtils.castToLong(proHashMap.get("PROMOTIONTYPE"));
                        if (null != proMotion) {
                            //跨库查询
                            JSONObject whereProDimJson = new JSONObject();
                            whereProDimJson.put("ID", proMotion);
                            HashMap proDimHashMap = QueryCpUtil.queryCP("PS_C_PRODIM_ITEM",
                                    "ENAME", whereProDimJson);
                            if (null != proDimHashMap && proDimHashMap.size() > 0) {
                                //推广类型
                                itemJson.put("PS_C_PRO_PROMOTIONTYPE", proDimHashMap.get("ENAME"));
                            }
                        }

                        Long pronature = TypeUtils.castToLong(proHashMap.get("PRONATURE"));
                        if (null != pronature) {
                            //跨库查询
                            JSONObject whereProDimJson = new JSONObject();
                            whereProDimJson.put("ID", pronature);
                            HashMap proDimHashMap = QueryCpUtil.queryCP("PS_C_PRODIM_ITEM",
                                    "ENAME", whereProDimJson);
                            if (null != proDimHashMap && proDimHashMap.size() > 0) {
                                //货品性质
                                itemJson.put("PS_C_PRO_PRONATURE", proDimHashMap.get("ENAME"));
                            }
                        }
                    } else {
                        throw new NDSException("商品档案 查询为空！  商品ID：" + h.get("PS_C_PRO_ID"));
                    }
                }
            }

            if (itemJson.containsKey("PRO_TYPE_DEPART_ID") &&
                    (null != itemJson.getLong("PRO_TYPE_DEPART_ID"))) {
                //跨库查询
                JSONObject whereProDimJs = new JSONObject();
                whereProDimJs.put("ID", itemJson.getLong("PRO_TYPE_DEPART_ID"));
                HashMap proDimHashMap = QueryCpUtil.queryCP("PS_C_PRODIM_ITEM",
                        "ECODE,ENAME", whereProDimJs);
                if (null != proDimHashMap && proDimHashMap.size() > 0) {
                    //品类编码
                    itemJson.put("PRO_TYPE_DEPART_ECODE", proDimHashMap.get("ECODE"));
                    //品类名称
                    itemJson.put("PRO_TYPE_DEPART_ENAME", proDimHashMap.get("ENAME"));
                }
            }

            String empecode = itemJson.getString("CP_C_SALER_ECODE");
            if (null != empecode) {
                //跨库查询
                JSONObject whereEmp = new JSONObject();
                whereEmp.put("ECODE", empecode);
                HashMap empHashMap = QueryCpUtil.queryCP("CP_C_EMP",
                        "ENAME,ID", whereEmp);
                if (null != empHashMap && empHashMap.size() > 0) {
                    //营业员ID
                    itemJson.put("CP_C_SALER_ID", empHashMap.get("ID"));
                    //营业员姓名
                    itemJson.put("CP_C_SALER_ENAME", empHashMap.get("ENAME"));
                }
            }
        }

        for (int j = 0; j < payitemJS.size(); j++) {
            JSONObject payitemJson = payitemJS.getJSONObject(j);
            payitemJson.put("ID", -1);
            payitemJson.put("ISACTIVE", "Y");

            String payEcode = payitemJson.getString("PAY_WAY_CODE");
            if (null == payEcode) {
                throw new NDSException("付款方式编码不能为空!");
            } else {
                HashMap wayHash = viewDLRetailMapper.selPaymentID(payEcode);
                if (null != wayHash) {
                    if (null == wayHash || wayHash.size() <= 0) {
                        throw new NDSException("付款方式查询为空!  payEcode:" + payEcode);
                    }
                    //付款方式ID
                    payitemJson.put("PAY_WAY_ID", wayHash.get("ID"));
                    //付款方式名称
                    payitemJson.put("PAY_WAY_NAME", wayHash.get("ENAME"));
                    if ("3".equals(payEcode)) {
                        payitemJson.put("COUPON_NO", payitemJson.getString("PAY_NO"));
                        payitemJson.put("PAY_NO", null);
                    }
                }
            }
        }

        /*************处理主表数据************/

        JSONObject newzbJson = new JSONObject();
        Set<String> keySet = zbJS.keySet();
        for (String key : keySet) {
            if ("OPR_ECODE".equals(key) || "IDLIST".equals(key) || "AUTO_CHARGE".equals(key)) {
                continue;
            } else {
                newzbJson.put(key, zbJS.get(key));
            }
        }
//        newzbJson.put("CREATIONDATE", new Timestamp(System.currentTimeMillis()).toInstant());
        newzbJson.remove("CREATIONDATE");
        newzbJson.put("ISACTIVE", "Y");
        newzbJson.put("SALE_TYPE", null);
        newzbJson.put("BILL_ADD_TYPE", 3);
        newzbJson.put("PAY_CHANGE", 0);
        newzbJson.remove("STATUSNAME");
        newzbJson.remove("STATUSTIME");

        String retail = zbJS.getString("RETAIL_TYPE");
        if ("POS".equals(retail)) {
            newzbJson.put("RETAIL_TYPE", 1);
        } else {
            newzbJson.put("RETAIL_TYPE", 0);
        }

        if (zbJS.containsKey("CP_C_STORE_ECODE") && (null != zbJS.getString("CP_C_STORE_ECODE"))) {
            String ecode = zbJS.getString("CP_C_STORE_ECODE");
            JSONObject whereJs = new JSONObject();
            whereJs.put("ECODE", ecode);
            //跨库查询
            HashMap h = QueryCpUtil.queryCP("CP_C_STORE", "ID", whereJs);
            if (null != h && h.size() > 0) {
                //门店ID
                newzbJson.put("CP_C_STORE_ID", h.get("ID"));
            } else {
                throw new NDSException("店仓 查询为空   店仓编码:" + ecode);
            }
        }

        String saveUserName = zbJS.getString("OWNERNAME");
        User user = queryUserUtil.getUser(saveUserName);

        if (zbJS.containsKey("VP_C_VIP_ECODE") && (null != zbJS.getString("VP_C_VIP_ECODE"))) {
            String ecode = zbJS.getString("VP_C_VIP_ECODE");

            DefaultWebEvent event = new DefaultWebEvent("test", new HashMap(16));
            QuerySessionImpl querySession = new QuerySessionImpl(user);
            //查询VIP档案ID
            JSONObject vipSearchJson = new JSONObject();
            //fixedcolumns
            JSONObject fixedcolumnsJson = new JSONObject();
            fixedcolumnsJson.put("ACC.ECODES", ecode);

            JSONArray searchTypeArray = new JSONArray();
            JSONObject ecodeName = new JSONObject();
            ecodeName.put("name", "ACC.ECODES");
            ecodeName.put("type", "exact");
            searchTypeArray.add(ecodeName);

            vipSearchJson.put("fixedcolumns", fixedcolumnsJson);
            vipSearchJson.put("searchtype", searchTypeArray);

            event.put("param", vipSearchJson);
            querySession.setEvent(event);

            String vipnotic = "com.jackrain.nea.vp.api.CvipSearchCmd:1.0:vp";
            JSONObject vipeRetJson = postDubbleEvent.executePostDubble(vipnotic, querySession);

            accesLogger.info("ViewDLRetailSaveCmdImpl--vipeRetJson-->{}", vipeRetJson);
//            log.info("vipeRetJson:" + vipeRetJson);
            if (null == vipeRetJson || (!vipeRetJson.containsKey("IDS"))) {
//                throw new NDSException("未查询到VIPID   " + vipeRetJson);
                newzbJson.put("VP_C_VIP_ACC_ID", null);
            }

            JSONArray idsArray = vipeRetJson.getJSONArray("IDS");
            if (null == idsArray || idsArray.size() <= 0) {
//                throw new NDSException("未查询到VIPID   ecode:" + ecode);
                newzbJson.put("VP_C_VIP_ACC_ID", null);
            } else {
                Long vipID = idsArray.getLong(0);
                //账户ID
                newzbJson.put("VP_C_VIP_ACC_ID", vipID);
            }
        }

        newzbJson.put("STATUS", 1);

        JSONObject fixColumn = new JSONObject();
        fixColumn.put("DL_B_RETAIL", newzbJson);
        fixColumn.put("DL_B_RETAIL_ITEM", itemJS);
        fixColumn.put("DL_B_RETAIL_PAY_ITEM", payitemJS);


        JSONObject resultJson = new JSONObject();
        resultJson.put("objid", -1);
        resultJson.put("IS_F4CHECK", "N");
        resultJson.put("fixcolumn", fixColumn);
        resultJson.put("table", "DL_B_RETAIL");

//        log.info("resultJson==>" + resultJson.toString());

        return resultJson;
    }


    /**
     * ES查询
     *
     * @param billNo 单据编号
     * @return id
     */
    public JSONObject es(String billNo) {
        JSONObject bill_no = esUtil.generateMatch("BILL_NO", billNo);
        List<JSONObject> terms = esUtil.packageTerm(bill_no);
        String esIndex = pconf.getProperty("DL_B_RETAIL");
        JSONObject jsonObject = null;
        try {
            jsonObject = esUtil.searchAllDataByTerms(esIndex,
                    esIndex, "1", "10000", terms);
            log.debug("es_jsonObject->" + jsonObject);
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        return jsonObject;
    }

}
